<template>
  <div class="app-container">
    <el-upload
      ref="upload"
      :action="action"
      :headers="{'authorization': 'Bearer ' + token}"
      :multiple="true"
      list-type="picture-card"
      :on-success="onUploadSuccess"
      :auto-upload="false">
      <i slot="default" class="el-icon-plus"></i>
      <div slot="file" slot-scope="{file}">
        <img
          class="el-upload-list__item-thumbnail"
          :src="file.url"
          alt="">
        <span class="el-upload-list__item-actions">
          <span
            class="el-upload-list__item-preview"
            @click="handlePictureCardPreview(file)"
          >
            <i class="el-icon-zoom-in"></i>
          </span>
          <span
            v-if="!disabled"
            class="el-upload-list__item-delete"
            @click="handleDownload(file)"
          >
            <i class="el-icon-download"></i>
          </span>
          <span
            v-if="!disabled"
            class="el-upload-list__item-delete"
            @click="handleRemove(file)"
          >
            <i class="el-icon-delete"></i>
          </span>
        </span>
      </div>
    </el-upload>
    <el-dialog :visible.sync="dialogVisible">
      <img width="100%" :src="dialogImageUrl" alt="">
    </el-dialog>
    <el-button style="margin-left: 10px; margin-top: 20px" size="medium" type="success" @click="submitUpload">上传到服务器</el-button>
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
export default {
  name: 'ImageAdd',
  data() {
    return {
      dialogImageUrl: '',
      dialogVisible: false,
      disabled: false,
      action: `${process.env.VUE_APP_BASE_API}/cms/swiper/create`
    }
  },
  computed: {
    ...mapGetters(['token'])
  },
  methods: {
    submitUpload() {
      this.$refs.upload.submit()
    },
    onUploadSuccess(response) {
      this.$message({
        message: response.message,
        type: 'success'
      })
    },
    handleRemove(file) {
      console.log(file)
    },
    handlePictureCardPreview(file) {
      this.dialogImageUrl = file.url
      this.dialogVisible = true
    },
    handleDownload(file) {
      console.log(file)
    }
  }
}
</script>
